package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.VariousKindsVideoBo;
import com.ruoyi.system.domain.vo.VariousKindsVideoVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.VariousKindsVideo;
import com.ruoyi.system.mapper.VariousKindsVideoMapper;
import com.ruoyi.system.service.IVariousKindsVideoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-23
 */
@RequiredArgsConstructor
@Service
public class VariousKindsVideoServiceImpl implements IVariousKindsVideoService {

    private final VariousKindsVideoMapper baseMapper;

    /**
     * 查询视频
     *
     * @param id 视频主键
     * @return 视频
     */
    @Override
    public VariousKindsVideoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询视频列表
     *
     * @param bo 视频
     * @return 视频
     */
    @Override
    public TableDataInfo<VariousKindsVideoVo> queryPageList(VariousKindsVideoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VariousKindsVideo> lqw = buildQueryWrapper(bo);
        Page<VariousKindsVideoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询视频列表
     *
     * @param bo 视频
     * @return 视频
     */
    @Override
    public List<VariousKindsVideoVo> queryList(VariousKindsVideoBo bo) {
        LambdaQueryWrapper<VariousKindsVideo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VariousKindsVideo> buildQueryWrapper(VariousKindsVideoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VariousKindsVideo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVideoId()), VariousKindsVideo::getVideoId, bo.getVideoId());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoUserId()), VariousKindsVideo::getVideoUserId, bo.getVideoUserId());
        lqw.like(StringUtils.isNotBlank(bo.getVideoUserName()), VariousKindsVideo::getVideoUserName, bo.getVideoUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoSourceWebsite()), VariousKindsVideo::getVideoSourceWebsite, bo.getVideoSourceWebsite());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoAddress()), VariousKindsVideo::getVideoAddress, bo.getVideoAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoTitle()), VariousKindsVideo::getVideoTitle, bo.getVideoTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoType()), VariousKindsVideo::getVideoType, bo.getVideoType());
        lqw.eq(StringUtils.isNotBlank(bo.getVideoPlatform()), VariousKindsVideo::getVideoPlatform, bo.getVideoPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getWxGroup()), VariousKindsVideo::getWxGroup, bo.getWxGroup());
        return lqw;
    }

    /**
     * 新增视频
     *
     * @param bo 视频
     * @return 结果
     */
    @Override
    public Boolean insertByBo(VariousKindsVideoBo bo) {
        VariousKindsVideo add = BeanUtil.toBean(bo, VariousKindsVideo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改视频
     *
     * @param bo 视频
     * @return 结果
     */
    @Override
    public Boolean updateByBo(VariousKindsVideoBo bo) {
        VariousKindsVideo update = BeanUtil.toBean(bo, VariousKindsVideo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(VariousKindsVideo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除视频
     *
     * @param ids 需要删除的视频主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public VariousKindsVideoVo selectRandomVideo() {
        return BeanCopyUtils.copy(baseMapper.selectRandomVideo(), VariousKindsVideoVo.class);
    }
}
